/**
 * 
 */
package com.buss.core.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.buss.core.model.Organization;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.ResultList;
import com.buss.core.service.OrganizationService;
import com.buss.core.sysparam.SysStaticParam;
import com.buss.core.validator.ResultMessage;

/**
 * 组织机构Controller
 * @author 高小勇
 * 创建时间:2011-5-24
 * wcms2.0
 */
@Controller
@RequestMapping("/admin")
public class OrganizationController extends BaseController {

	@Autowired
	private OrganizationService organizationService;
	
	/**
	 * 机构列表页面
	 * @return
	 */
	@RequestMapping("/organization/page")
	public ModelAndView listPage(@RequestParam(required=false) String menuid){
		ModelAndView view = new ModelAndView("organization/listpage");
		view.addObject(SysStaticParam._MENUID_NAME, menuid);
		return view;
	}
	
	/**
	 * 新增、修改页面
	 * @param type
	 * @param id
	 * @return
	 */
	@RequestMapping("/organization/{type}")
	public ModelAndView editPage(@PathVariable String type,@RequestParam(required=false) Long id){
		ModelAndView view = new ModelAndView("organization/edit");
		Organization organ = new Organization();
		Organization parentOrgan = null;
		if("editpage".equals(type)){
			organ = organizationService.loadOrganization(id);
			if(organ.getParentId() != 0){
				parentOrgan = organizationService.loadOrganization(organ.getParentId());
			}
		}else{
			if(id != 0){
				parentOrgan = organizationService.loadOrganization(id);
			}			
		}
		if(parentOrgan == null){
			parentOrgan = new Organization();
			parentOrgan.setId(0L);
			parentOrgan.setName("组织机构");
		}
		view.addObject("pOrgan", parentOrgan);
		view.addObject("organ", organ);
		return view;
	}
	
	/**
	 * 新增组织机构信息
	 * @param organ
	 * @return
	 */
	@RequestMapping("/organization/add")
	@ResponseBody
	public Map<String, Object> add( Organization organ,HttpServletRequest request){
		organ.setCompanyId(getCompanyId(request));
		Organization tmpOrgan = organizationService.mergeOrganization(organ);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("organ", tmpOrgan);
		map.put("message", new ResultMessage("0", "新增组织机构信息成功!"));
		return map;
	}
	
	/**
	 * 修改组织机构信息
	 * @param organ
	 * @return
	 */
	@RequestMapping("/organization/edit")
	@ResponseBody
	public Map<String,Object> edit( Organization organ,HttpServletRequest request){
		organ.setCompanyId(getCompanyId(request));
		organizationService.editOrganization(organ);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("message", new ResultMessage("0", "修改组织机构信息成功!"));
		return map;
	}
	
	/**
	 * 删除组织机构信息
	 * @param ids
	 * @return
	 */
	@RequestMapping("/organization/delete")
	@ResponseBody
	public Map<String,Object> delete(@RequestParam Long[] ids){
		organizationService.deleteOrganization(ids);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("message", new ResultMessage("0", "删除组织机构信息成功!"));
		return map;
	}
	
	/**
	 * 获取组织机构列表
	 * @param parentId
	 * @return
	 */
	@RequestMapping("/organization/list/{parentId}")
	@ResponseBody
	public Map<String,Object> list(String orderField,String orderType,@PathVariable Long parentId,HttpServletRequest request){
		OrderBy orderBy = new OrderBy();
		orderBy.add(orderField, orderType);
		ResultList<Organization> resultList = organizationService.queryByParentId(getCompanyId(request),parentId,orderBy);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", resultList);
		map.put("message", new ResultMessage("0", "获取组织机构信息成功!"));
		return map;
	}
	
	/**
	 * 获取组织机构树形列表
	 * @param parentId
	 * @return
	 */
	@RequestMapping("/organization/listByTree/{parentId}")
	@ResponseBody
	public Map<String,Object> listByTree(String orderField,String orderType,@PathVariable Long parentId,HttpServletRequest request){
		OrderBy orderBy = new OrderBy();
		orderBy.add(orderField, orderType);
		ResultList<Organization> resultList = organizationService.queryByParentId(getCompanyId(request),parentId,orderBy);
		queryChild(resultList.getData(),getCompanyId(request),orderBy);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("result", resultList);
		map.put("message", new ResultMessage("0", "获取组织机构树成功!"));
		return map;
	}
	
	private void queryChild(List<Organization> organs,String companyId,OrderBy orderBy){
		for(Organization organ : organs){
			ResultList<Organization> resultList = organizationService.queryByParentId(companyId, organ.getId(),orderBy);
			organ.setNodes(resultList.getData());
			queryChild(resultList.getData(),companyId,orderBy);
		}
	}

}
